package com.yyx.app1.CommonControls.EditTextItemControl;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yyx.app1.R;

import org.androidannotations.annotations.AfterTextChange;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

/**
 * Created by yuexing on 2015/9/10.
 */
@EViewGroup(R.layout.control_edittextitem)
public class EditTextItemControl extends LinearLayout {
    private OnEditTextItemControl_EditTextChange_Listener EditTextChange_Listener;
    private int oldcolor = Color.BLACK;
    private int newcolor = Color.WHITE;

    @ViewById
    TextView control_textview,control_clear;

    @ViewById
    EditText control_edittext;

    @ViewById
    LinearLayout control_layout;

    public EditTextItemControl(Context context) {
        super(context);
    }

    public EditTextItemControl(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EditTextItemControl(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void set_Color_Change(boolean b) {
        if (!b)
        {
            int temp=oldcolor;
            oldcolor=newcolor;
            newcolor=temp;
        }
        control_clear.setTextColor(oldcolor);
    }

    public void setNewcolor(int newcolor) {
        this.newcolor = newcolor;
    }

    public void setOldcolor(int oldcolor) {
        this.oldcolor = oldcolor;
    }

    public void setEditTextChange_Listener(OnEditTextItemControl_EditTextChange_Listener editTextChange_Listener) {
        EditTextChange_Listener = editTextChange_Listener;
    }

    public TextView getControl_textview() {
        return control_textview;
    }

    public TextView getControl_clear() {
        return control_clear;
    }

    public EditText getControl_edittext() {
        return control_edittext;
    }

    public LinearLayout getControl_layout() {
        return control_layout;
    }

    @AfterTextChange(R.id.control_edittext)
    public void edittextchange()
    {
        if (EditTextChange_Listener!=null)
        {
            EditTextChange_Listener.edit_text_change();
        }
    }

    @Click
    public void control_clear()
    {
        control_clear.setTextColor(newcolor);
        control_edittext.setText("");
        new Handler().postDelayed(r, 300);
    }
    Runnable r = new Runnable() {
        @Override
        public void run() {
            control_clear.setTextColor(oldcolor);
        }
    };

    public double getControl_EDitText_content_for_double()
    {
        try {
            double result = Double.parseDouble(control_edittext.getText().toString());
            return result;
        }catch (Exception e)
        {
            Log.i("Message","double转化失败!");
            return 0;
        }
    }
}
